package com.tms.admin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.tms.admin.service.CaptchaService;
import com.tms.common.entity.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.concurrent.ThreadLocalRandom;

/*-----------------------------------
 *   @Time: 2022/9/5 16:48
 *   @Description: 验证码
-----------------------------------*/
@Api(value = "验证码管理")
@RestController()
@RequestMapping("/captcha")
@CrossOrigin
@Slf4j
public class CaptchaController {

    @Autowired
    private CaptchaService captchaService;
    @ApiOperation(value = "获取验证码的base64编码,值会保存到自定义的缓存")
    @GetMapping("{username}")
    public ResultVO getCode(@PathVariable String username) throws IOException {
        String captcha = captchaService.getCode(username);
        return ResultVO.ok().data("item", captcha);
    }
    @ApiOperation(value = "验证码验证")
    @GetMapping("{username}/{captcha}")
    public ResultVO verify(
            @PathVariable String captcha,
            @PathVariable String username) {
        captchaService.verify(username, captcha);
        return ResultVO.ok();
    }
}
